<script setup lang="ts">
  import { PropType } from 'vue'
  import { useCheckAllotOutWarehouse } from '@/services/pages/storage/useWarehouse'
  import { HMessage, HMessageBox } from '@/components/common/message'

  const { checkForm, checkLoading, check, resetForm } = useCheckAllotOutWarehouse()

  const props = defineProps({
    form: {
      type: Object as PropType<StorageAPIRequest.AllotWarehouseInOrOutForm>,
      required: true,
    },
    outWarehouseId: {
      type: String,
      required: true,
    },
  })

  const emits = defineEmits<{
    (e: 'update'): void
  }>()

  // 弹窗显示状态
  const visible = defineModel('modelValue', { type: Boolean, default: false })

  /**
   * 隐藏弹窗
   */
  const hideDialog = () => {
    resetForm()
    visible.value = false
  }

  /**
   * 确定审核结果
   * @param {1|2} checkResult - 审核结果，1-通过，2-驳回
   */
  const handleConfirmCheckResult = async (checkResult: 1 | 2) => {
    checkForm.value.checkResult = checkResult
    checkForm.value.address = props.form.address
    checkForm.value.area = props.form.area
    checkForm.value.city = props.form.city
    checkForm.value.phone = props.form.phone
    checkForm.value.province = props.form.province
    checkForm.value.remark = props.form.remark
    checkForm.value.typeId = props.form.typeId
    checkForm.value.typeName = props.form.typeName || '调拨入库'
    checkForm.value.username = props.form.username
    checkForm.value.storeMaterialVos = props.form.storeMaterialVos
    const res = await check(props.outWarehouseId)
    if (res.code === 20000) {
      HMessage.success('审核完成')
      emits('update')
      hideDialog()
      return
    }
    HMessageBox.error(res.message)
  }
</script>

<template>
  <h-dialog v-model="visible" title="审核调拨出库" :loading="checkLoading" @close="hideDialog">
    <el-form class="modal_form_content" inline-message label-position="top">
      <el-form-item label="物流信息">
        <el-input
          v-model.trim="checkForm.checkRemark"
          type="textarea"
          maxlength="200"
          show-word-limit
          placeholder="请输入物流信息"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <h-button is-modal type="primary" @click="handleConfirmCheckResult(1)">通过</h-button>
      <h-button is-modal type="error" @click="handleConfirmCheckResult(2)">驳回</h-button>
      <h-button is-modal @click="hideDialog">取消</h-button>
    </template>
  </h-dialog>
</template>

<style scoped lang="scss">
  :deep(.el-textarea__inner) {
    height: 100px;
  }
</style>
